package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.RankingFragmentAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.Ranking;
import com.lanou.musicbox.bean.RankingContent;
import com.lanou.musicbox.utils.StringRequestUtf;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class RankingListFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private StringRequest stringRequest;
    private List<RankingContent> rankingContents;
    private VolleySingleton volleySingleton;
    private RankingFragmentAdapter adapter;
    private Ranking ranking;
    private FragmentManager manager;
    private String TAG =  "lanou33";
    private String url_ranking =
            "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.billboard.billCategory&format=json&kflag=1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_ranking,null);
        listView = (ListView) v.findViewById(R.id.ranking_listview);
        listView.setOnItemClickListener(this);
        Log.i(TAG, "onCreateView ");
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initDatas();
    }


    //解析数据
    private void initDatas() {
         stringRequest = new StringRequestUtf(url_ranking, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 Gson gson = new Gson();
               ranking = gson.fromJson(response,Ranking.class);
                 Log.i("lanou33", "onResponse "+response);

                 if (ranking.getContent()!= null ){
                     rankingContents = ranking.getContent();
                    // Log.i("lanou333", "log"+rankingContents.get(0).getName());
                     adapter = new RankingFragmentAdapter(rankingContents,getActivity());
                     listView.setAdapter(adapter);
                 }

             }
         }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError volleyError) {

                 Log.i(TAG, "onErrorResponse ");
             }
         });

        volleySingleton.addQueue(stringRequest);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i("lanou00", "onItemClick ");
   // Ranking ranking = (Ranking) parent.getItemAtPosition(position);
        int type = ranking.getContent().get(position).getType();
        Log.i("lanou88889", "onItemClick_type "+type);
        manager = getActivity().getSupportFragmentManager();
        RankingMusicFragment fragment = new RankingMusicFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type_id", type + "");
        fragment.setArguments(bundle);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.fill_main, fragment);
        transaction.commit();

    }
}
